#!/usr/bin/perl
#transcript.pl
#transcribes a text using the boss_g2p_de and provides the output 
#formatted for coding_scripts_for_lam
#options:
#-sent takes the path to the inputfile
#-h prints $USAGE
#-createIDs creates sentence IDs in descendent order using line numbers as identifiers
#-corr activates the correction mode. in this mode the program prints out another file which contains the 
#orthographic sentence and the transcirbed sentence and a line of marcers, which indicates which word has been 
#trnascribed via the dictionary and which via the g2p

#TODO: put out a file which indicates which transcriptions are from lexicon and which from g2p
#TODO: which g2p to use
#TODO: accept sentences from the commandline to transcribe (doesn't the lexlookup tool do it already?)


#Version: 1.1
#@autor: Igor Jauk
#Changelist:
#config class support
#lex class support instead of lexcheck.pl
#standard output file creation
#minor code improvements

use FindBin qw($Bin);
use lib "$Bin/../libs";
use config;
use lex;

my $USAGE = 'perl transcript.pl -sent SENTENCEFILE [-lex [LEXPATH] -g2p G2PPROGRAM -createIDs -corr -h]';
my @content;
my @transcription;
my @ID;
my @corr;
main();

sub main {
	my $cl = config->new(\@ARGV);
	if ($cl->optionflag("-h")) {
		print $USAGE."\n";
		exit;
	}

	my $lex;
	if ($cl->optionflag('-lex')) {
		$lex = lex->new(\@ARGV);
		print STDERR "Using a lexicon lookup first\n";
		print STDERR "Using lexicon: ".$lex->getLexPath()."\n";
	}
	my $g2p = 'boss_g2p_de';
	if ($cl->optionflag('g2p')) {
		$g2p = $cl->optionarg('g2p');
	}
	print STDERR "Using g2p program: ".$g2p."\n";

	my $in;
	if (!$cl->optionflag('-sent') || $cl->optionarg('-sent') eq "") {
		die "error: no input file specified\n";
	} else {
		$in = $cl->optionarg('-sent');
	}
	
	print STDERR "Creating outputfile \'transcription.txt\'\n";
	my $output = 'transcription.txt';
	
	open IN, "$in" or die "could not open the input file $in. aborting\n";
	chomp(my @input = <IN>);
	close(IN);

	if ($cl->optionflag('-createIDs')) {
		createIDs(\@input);
	}
	getSubcontents(\@input);
	transcribe($g2p, \$lex, \$cl);

	open OUT, ">$output" or die "could not create output file. aborting\n";
	open CORR, ">$output.corr" or die "could not create the correction file\n" if ($cl->optionflag('-corr'));
	for (my $i=0; $i<@content; $i++) {
		my @buff = split(/ +/,$content[$i]);
		if ($buff[0] =~  /\(\d+\)/) {
			$content[$i] = '';
			for (my $j=1; $j<@buff; $j++) {
				$content[$i] = $content[$i].' ' if ($j>1);
				$content[$i] = $content[$i].$buff[$j];
			}
		}
		print OUT $ID[$i]." ";
		$transcription[$i] =~ s/\n/ /g;
		$transcription[$i] =~ s/primary//g;
		$transcription[$i] =~ s/secondary//g;
		$transcription[$i] =~ s/"/'/g;
		$transcription[$i] =~ s/ +/ /g;
		$content[$i] =~ s/ +/ /g;
		print OUT $transcription[$i]."\n";
		print OUT $content[$i]."\n";
		if ($cl->optionflag('-corr')) {
			#print CORR $ID[$i]." ";
			print CORR $transcription[$i]."\n";
			print CORR $content[$i]."\n";
			print CORR $corr[$i]."\n\n";
		}
	}
	close(OUT);
	close(CORR);
}

sub getSubcontents {
	#$_[0]: input
	my $c = -1;
	foreach $line (@{$_[0]}) {
		my @buff = split(/ +/, $line);
		if ($buff[0] =~ /\(\d+\)/) {
			$c++;
			$content[$c] = $line;
			$ID[$c] = $buff[0];
		} else {
			$c++ if ($c==-1);
			$content[$c] = $content[$c].' ' if (defined $content[$c]);
			$content[$c] = $content[$c].$line;
		}
	}
	if ($c==0) {
		print "Warning: no sentence identifier found. Using whole text as content of one file.\n";
		$ID[0] = '(001)';
	}	
}

sub createIDs {
	#$_[0]: input
	for (my $i=0; $i<@{$_[0]}; $i++) {
		my $line = $i+1;
		$_[0]->[$i] = "($line) ".$_[0]->[$i];
	}
}

sub transcribe {
	#$_[0]: $g2p, $_[1]: $lex, $_[2]: $cl
	my ($g2p, $lex, $cl) = @_;
	my $c = 0;
	my $i = -1;
	foreach $line (@content) {
		my @trans;
		my $c=0;
		my @cor;
		$i++;
		my @buff = split(/ +/, $line);
		if ($buff[0] =~ /\(\d+\)/) {
			shift(@buff);
		}
		for (my $b=0; $b<@buff; $b++) {
			$buff[$b] =~ s/[\(\),\.!\?]//g;
			if ($buff[$b] !~  /\(\d+\)/) {
				if ($$cl->optionflag('-lex')) {
					$trans[$c] = $$lex->checkEntry($buff[$b]);
					if ($$cl->optionflag('-corr') && $trans[$c] ne 'no transcription found') {
						$cor[$c] = 'LEX';
					}
					if ($$cl->optionflag('-corr') && $trans[$c] =~ /\|\|\|\|/) {
						$cor[$c] = 'MULTILEX';
					}
				}
				if (!$$cl->optionflag('-lex') || $trans[$c] eq 'no transcription found') {
					chomp($trans[$c] = qx($g2p $buff[$b]));
					if ($$cl->optionflag('-corr') && defined $trans[$c]) {
						$cor[$c] = 'G2P';
					}
				}
				$c++;
			}
			$transcription[$i] = $transcription[$i].' ' if ($b>0);
			$transcription[$i] = $transcription[$i].'#'.$trans[$c-1];
			if ($$cl->optionflag('-corr')) {
				$corr[$i] = $corr[$i].' ' if ($c>1);
				$corr[$i] = $corr[$i].$cor[$c-1];
			}
		}
	}
}
